<template>
  <div class="container">
    <div class="forum-title">{{landlord.title}}</div>
    <!--<div class="comments">13评论</div>-->
    <div class="floor">
    <div class="forum-floor">
      <div class="forum-floor-top">
        <img v-if="!landlord.pic" class="floor-user-thumb" src="@/assets/images/userimg.png"/>
        <img  class="floor-user-thumb" v-else :src="landlord.pic"/>
        <div class="forum-floor-topright">
          <span class="user-name">{{landlord.poster}}</span>
          <span class="create-time">1楼 {{landlord.posttime}}</span>
        </div>
      </div>
      <div class="forum-floor-bottom">
        <p v-html="landlord.content"></p>
      </div>
    </div>

      <van-list
        v-model="loading"
        :finished="finished"
        :immediate-check="false"
        finished-text="没有更多了"
        @load="onLoad"
      >
    <div class="forum-floor" v-for="(item, index) in itemList">
      <div class="forum-floor-top">
        <img v-if="!item.pic" class="floor-user-thumb" src="@/assets/images/userimg.png"/>
        <img  class="floor-user-thumb" :src="item.pic" v-else />
        <div class="forum-floor-topright">
          <span class="user-name">{{item.lastuser}}</span>
          <span class="create-time">{{index + 2}}楼 {{item.lasttime}}</span>
        </div>
      </div>
      <div class="forum-floor-bottom">
        <p v-html="item.content"></p>
      </div>
    </div>
      </van-list>

    </div>
    <div class="btn-wrapper">
      <button class="exam-btn" @click="editorComment">编辑评论</button>
    </div>
    <van-dialog
      v-model="showComment"
      title="编辑评论"
      :beforeClose="beforeClose"
      show-cancel-button
      confirm-button-text="发表"
    >
      <div class="container">
          <van-field
            v-model="commitTitle"
            name="title"
            label="标题"
            placeholder="请填写标题"
            :rules="[{ required: true, message: '请填写标题' }]"
          />

          <van-field
            v-model="commitCon"
            name="content"
            rows="3"
            autosize
            label=""
            type="textarea"
            placeholder="请输入内容"
          />

      </div>
    </van-dialog>

  </div>
</template>

<script>
  import util from '@/common/util';
  export default {
    data () {
      return {
        showComment: false,
        id: '',
        pic:'',
        stname: '',
        page: 1,
        total: 0,
        itemList: [],
        landlord: '',
        baseURL: '',
        commitTitle: '',
        commitCon: '',
        loading: false,
        finished: false,
      }
    },
    created() {
      if(util.storage.get('userinfo')){
        this.pic = util.storage.get('userinfo').pic;
        this.stname = util.storage.get('userinfo').stname;
      }
      this.id = this.$route.query.id;
      this.baseURL = util.baseURL;
      console.log(this.id)
      this.getDetail();
    },
    methods: {
      getDetail() {
        let that = this;
        that.$api.post('/mobile/Stmsg/ltdetail',{"user":{"id": that.id, "page":that.page}}, r => {
          //console.log('论坛内容', r.data);
          that.landlord = r.data.qzls;
          let rows = r.data.hfls; //请求返回当页的列表
          that.loading = false;
          //that.total = res.data.total;  获取总条数
          console.log('rows',rows);
          if (rows == null || rows.length === 0) {
            this.finished = true; // 加载结束
            return;
          }
          // 将新数据与老数据进行合并
          that.itemList = that.itemList.concat(rows);
          //console.log('itemList', that.itemList)
        })
      },
      onLoad() { //下拉加载更多
        this.page++;
        this.getDetail();
      },
      editorComment() {
        let that = this;
        that.showComment = true;
      },
      beforeClose(action, done) {
        let that = this;
        if(action === 'confirm'){
          if(!that.commitTitle){
            that.$toast('标题不能为空！')
            done(false)
            return false;
          }else if (!that.commitCon) {
            that.$toast('内容不能为空！')
            done(false)
            return false;
          }
          let formData = {
            hfid: that.id,
            title: that.commitTitle,
            content: that.commitCon,
            pic: that.pic,
            stname: that.stname
          };
          console.log('formData',formData)
          that.$api.post('/mobile/Stmsg/hftz', {'user':formData}, r => {
            console.log('回帖成功',r)
            if(r.code == 1){
              setTimeout(done, 500);
              this.$router.replace({
                path: '/refresh',
                query: {
                  id: r.data.hfid,
                  t: Date.now()
                }
              })
            }
          });

        } else if(action === 'cancel') {
          done() //关闭
        }
      }
    }
  }
</script>

<style scoped>
  .floor {
    padding-bottom: 60px;
  }
</style>
